#include "timingswitch.h"
#include "QFile"
#include "QDebug"
#include "QDomDocument"
#include "iostream"

TimingSwitch::TimingSwitch()
{
}

TimingSwitch* TimingSwitch::createTimingSwitch()
{
	static TimingSwitch* timingSwitch = new TimingSwitch;
	return timingSwitch;
}

void TimingSwitch::run()
{
	while(true)
	{
		bootTimingFile.setFileName("boottiming.xml");
		shutdownTimingFile.setFileName("shutdowntiming.xml");

		if(!bootTimingFile.exists() && !shutdownTimingFile.exists())
		{
			//qDebug()<<"no timing.";
			sleep(10);
		}
		if(bootTimingFile.exists())
		{
			//qDebug()<<"bootTimingFile Exist";
			QDomDocument bootTimingXml;
			if(!bootTimingXml.setContent(&bootTimingFile))
			{
				qDebug()<<"Parse BootTiming xml file error";
			}
			else
			{
				if(QString::compare("false",
									QString::fromLocal8Bit(bootTimingXml.firstChildElement().attribute("repeat").toStdString().c_str()),Qt::CaseInsensitive) == 0)
				{
					if(QString::compare(QTime::currentTime().toString("h:mm"),
										QString::fromLocal8Bit(bootTimingXml.firstChildElement().attribute("time").toStdString().c_str()),Qt::CaseInsensitive) == 0)
					{
					//	qDebug()<<"It is time to boot once";
						bootTimingFile.remove();
					}
					else
					{
				//		qDebug()<<"It is no time to boot once";
					}
				}
				if(QString::compare("true",
									QString::fromLocal8Bit(bootTimingXml.firstChildElement().attribute("repeat").toStdString().c_str()),Qt::CaseInsensitive) == 0)
				{
					int today = bootTimingXml.firstChildElement().attribute("day").toStdString().find(DateToWeek(QDate::currentDate()));
					if(today>= 0)
					{
					//	qDebug()<<"today is right day";
						if(QString::compare(QTime::currentTime().toString("h:mm"),
											QString::fromLocal8Bit(bootTimingXml.firstChildElement().attribute("time").toStdString().c_str()),Qt::CaseInsensitive) == 0)
						{
				//			qDebug()<<"It is time to boot again";
						}
						else
						{
					//		qDebug()<<"It is no time to boot again";
						}
					}
					else
					{
				//		qDebug()<<"today is wrong day";
					}
				}

			}
		}
		if(shutdownTimingFile.exists())
		{
		//	qDebug()<<"shutdownTimingFile Exist";
			QDomDocument shutdownTimingXml;
			if(!shutdownTimingXml.setContent(&shutdownTimingFile))
			{
				qDebug()<<"Parse ShutdownTiming xml file error";
			}
			else if(QString::compare("false",
									 QString::fromLocal8Bit(shutdownTimingXml.firstChildElement().attribute("repeat").toStdString().c_str()),Qt::CaseInsensitive) == 0)
			{
				if(QString::compare(QTime::currentTime().toString("h:mm"),
									QString::fromLocal8Bit(shutdownTimingXml.firstChildElement().attribute("time").toStdString().c_str()),Qt::CaseInsensitive) == 0)
				{
			//		qDebug()<<"It is time to shutdown once";
					shutdownTimingFile.remove();
					system("shutdown -s");
				}
				else
				{
		//			qDebug()<<"It is no time to shutdown once";
				}
			}
			if(QString::compare("true",
								QString::fromLocal8Bit(shutdownTimingXml.firstChildElement().attribute("repeat").toStdString().c_str()),Qt::CaseInsensitive) == 0)
			{
				int today = shutdownTimingXml.firstChildElement().attribute("day").toStdString().find(DateToWeek(QDate::currentDate()));
				if(today>= 0)
				{
			//		qDebug()<<"today is right day";
					if(QString::compare(QTime::currentTime().toString("h:mm"),
										QString::fromLocal8Bit(shutdownTimingXml.firstChildElement().attribute("time").toStdString().c_str()),Qt::CaseInsensitive) == 0)
					{
			//			qDebug()<<"It is time to shutdown again";
						system("shutdown -s");
					}
					else
					{
			//			qDebug()<<"It is no time to shutdown again";
					}
				}
				else
				{
		//		qDebug()<<"today is wrong day";
				}
			}
		}
		bootTimingFile.close();
		shutdownTimingFile.close();

		sleep(10);
	}

}

char TimingSwitch::DateToWeek(QDate date)
{
	QString today = date.toString("yyyyMMdd");
	QString year = today.left(4);
	QString month = today.mid(4,2);
	QString day = today.right(2);

	int m_year = year.toInt();
	int m_month = month.toInt();
	int m_day = day.toInt();

	int weekday = 0;
	int monthnum[2][13]={ {0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31} };
	char w_day[] = "01234567";

	for(int i = 1;i < m_year;i++)
	{
		if((i%4 == 0 && i%100 != 0) || i%400 ==0)
			weekday = (weekday + 366) % 7;
		else
			weekday = (weekday + 365) % 7;
	}

	for(int i = 1;i < m_month;i++)
	{
		if ((m_year%4 == 0 && m_year%100 != 0) || m_year%400==0)
			weekday=(weekday+monthnum[1][i])%7;
		else
			weekday=(weekday+monthnum[0][i])%7;
	}

	weekday += m_day;

	if (weekday%7==0)
		weekday=7;
	else
		weekday=weekday%7;

	return w_day[weekday];
}

void TimingSwitch::onRemoveFile(bool flag)
{
	if(flag)
	{
		bootTimingFile.close();
		bootTimingFile.remove();
	}
	else
	{
		shutdownTimingFile.close();
		shutdownTimingFile.remove();
	}
}
